package com.vick.tool;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.view.View;

import com.gen.tool.AlertDialog_PG;
import com.gen.tool.AlertDialog_myturn;
import com.gen.tool.AlertDialog_ok;
import com.gen.tool.SoundPlayer;
import com.ouken.com.LoginActivity;
import com.ouken.com.R;

public class DialogTool {
	


	public static void showCustomMessage(Context context, String message) {

		AlertDialog_ok myDialog = new AlertDialog_ok(context, message);
		final AlertDialog dialog = myDialog.AlertDialog_ok_show();
		myDialog.getConfirm_btn_ok().setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						SoundPlayer.playSound(R.raw.keypress_common);
						dialog.dismiss();
					}
				});

	}

	public static void showFlashMessage(Context context, String message) {
		AlertDialog_myturn myDialog = new AlertDialog_myturn(context, message);
		final AlertDialog dialog = myDialog.AlertDialog_myturn_show();
		new Handler().postDelayed(new Runnable() {
			public void run() {
				dialog.dismiss();
			}
		}, 1000);
	}
	
	public static AlertDialog_PG showProgressDialog(Context context,String message){
		AlertDialog_PG dialog=new AlertDialog_PG(context, message);
		dialog.AlertDialog_PG_show();
		return dialog;
	}
	
	public static void cancelProgressDialog(AlertDialog_PG progressDialog){
		progressDialog.getAlertDialog().dismiss();
	}

	public static boolean checkNetworkAlive(String result, Context context,
			DialogInterface.OnClickListener failListener) {
		
		if (result != null)
			return true;
		

		AlertDialog alertDialog = new AlertDialog.Builder(context)
				.setIcon(R.drawable.alert_dialog_icon).setTitle("ERROR!")
				.setMessage("Cant connect")
				.setPositiveButton("OK", failListener).create();

		alertDialog.show();
		return false;
	}

	public static boolean checkLogin(String result, Context context) {
		final Context ct = context;
		if ("ERROR,Please login first".equals(result)) {
			AlertDialog_ok myDialog = new AlertDialog_ok(context,
					"Sorry~>.<~ \nYou have double logged in~");
			final AlertDialog doublelog = myDialog.AlertDialog_ok_show();
			myDialog.getConfirm_btn_ok().setOnClickListener(
					new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							SoundPlayer.playSound(R.raw.keypress_common);
							SharedPreferences userInfo = ct
									.getSharedPreferences("user_info", 0);
							userInfo.edit().putString("AUTOLOGIN", "NO")
									.commit();
							doublelog.dismiss();

							Intent intent = new Intent(ct, LoginActivity.class);
							intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
							ct.startActivity(intent);
						}

					});

			return false;
		} else
			return true;
	}

}
